home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJSRC106.ARJ / XMS.C < prev    next >
C/C++ Source or Header  |  1992-04-13  |  6KB  |  304 lines

  1. /*
  2. ** xms.c -- C bindings for xms API - implemented
  3. ** Author: Kent Williams william@umaxc.weeg.uiowa.edu
  4. */
  5. #pragma inline
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include "xms.h"
  9.  
  10. static void far xms_spoof(void);
  11. static void (far *xms_entry)(void) = xms_spoof;
  12. char xms_error;
  13.  
  14. int
  15. xms_installed(void) {
  16.     asm    mov    ax,0x4300    /* get installation status */
  17.     asm    int 0x2f        /* call driver */
  18.     asm    mov    ah,1        /* return true if present */
  19.     asm    cmp al,0x80        /* are you there? */
  20.     asm    je __present
  21.     asm    xor ax,ax        /* no, return false */
  22.     __present:
  23.     asm    xchg    ah,al    /* if present, set ax to 1 */
  24.     asm    cbw
  25. }
  26.  
  27. static void
  28. xms_get_entry(void) {
  29.     asm    mov    ax,0x4310    /* get driver entry */
  30.     asm    int 0x2f        /* call multiplex */
  31.     asm    mov word ptr xms_entry,bx
  32.     asm    mov    word ptr (xms_entry+2),es
  33. }
  34.  
  35. static void far
  36. xms_spoof(void) {
  37.     asm    push ax
  38.     asm    push bx
  39.     asm    push cx
  40.     asm    push dx
  41.     asm    push si
  42.     asm    push di
  43.     asm    push es
  44.     asm    push ds
  45.     if(!xms_installed()) {
  46.         fputs("No XMS driver installed\n",stderr);
  47.         exit(1);
  48.     } else
  49.         xms_get_entry();
  50.     asm    pop ds
  51.     asm    pop    es
  52.     asm    pop    di
  53.     asm    pop    si
  54.     asm    pop    dx
  55.     asm    pop cx
  56.     asm    pop    bx
  57.     asm    pop    ax
  58.     xms_entry();
  59. }
  60.  
  61. xms_version_info *
  62. xms_get_version_info(void) {
  63.     unsigned xms_ver,xmm_ver,hma_indicator;
  64.     static xms_version_info x;
  65.     asm    xor ah,ah
  66.     asm    call [xms_entry]
  67.     asm    mov    xms_error,bl
  68.     asm    mov    xms_ver,ax
  69.     asm    mov xmm_ver,bx
  70.     asm    mov    hma_indicator,dx
  71.     xms_ver = (xms_ver & 0xf) + (((xms_ver >> 4) & 0xf) * 10) +
  72.             ((xms_ver >> 8) & 0xf) * 100 + ((xms_ver >> 12) & 0xf) * 1000;
  73.  
  74.     xmm_ver = (xmm_ver & 0xf) + (((xmm_ver >> 4) & 0xf) * 10) +
  75.             ((xmm_ver >> 8) & 0xf) * 100 + ((xmm_ver >> 12) & 0xf) * 1000;
  76.     x.xms_ver = xms_ver; x.xmm_ver = xmm_ver; x.hma_present = hma_indicator;
  77.     return &x;
  78. }
  79.  
  80. xms_extended_info *
  81. xms_query_extended_memory(void) {
  82.     unsigned max_free_block,total_extended_memory;
  83.     static xms_extended_info x;
  84.  
  85.     asm    mov    ah,0x8
  86.     asm    call [xms_entry]
  87.     asm    mov    xms_error,bl
  88.     asm    mov    max_free_block,ax
  89.     asm    mov    total_extended_memory,dx
  90.     x.max_free_block = max_free_block;
  91.     x.total_extended_memory = total_extended_memory;
  92.     return &x;
  93. }
  94.  
  95. int
  96. xms_hma_allocate(unsigned siz) {
  97.     asm    mov    ah,0x01
  98.     asm    mov    dx,word ptr siz
  99.     asm    call [xms_entry]
  100.     asm    mov    xms_error,bl
  101.         /* xms returns 1 on success, 0 on failure.  We need to invert this
  102.         ** for the C idiom.
  103.         ** if ax = 1 then not ax = 0xfffe and add ax,2 = 0
  104.         ** if ax = 0 then not ax = 0xffff and add ax,2 = 1
  105.         */
  106.     asm    not ax
  107.     asm    inc ax
  108.     asm    inc ax
  109. }
  110.  
  111. int
  112. xms_hma_free(void) {
  113.     asm    mov    ah,0x02
  114.     asm    call [xms_entry]
  115.     asm    mov    xms_error,bl
  116.     asm    not ax
  117.     asm    inc ax
  118.     asm    inc ax
  119. }
  120.  
  121.  
  122. int
  123. xms_global_enable_a20(void) {
  124.     asm    mov    ah,0x03
  125.     asm    call [xms_entry]
  126.     asm    mov    xms_error,bl
  127.     asm    not ax
  128.     asm    inc ax
  129.     asm    inc ax
  130. }
  131.  
  132. int
  133. xms_global_disable_a20(void) {
  134.     asm    mov    ah,0x4
  135.     asm    call [xms_entry]
  136.     asm    mov    xms_error,bl
  137.     asm    not ax
  138.     asm    inc ax
  139.     asm    inc ax
  140. }
  141.  
  142. int
  143. xms_local_enable_a20(void) {
  144.     asm    mov    ah,0x5
  145.     asm    call [xms_entry]
  146.     asm    mov    xms_error,bl
  147.     asm    not ax
  148.     asm    inc ax
  149.     asm    inc ax
  150. }
  151.  
  152. int
  153. xms_local_disable_a20(void) {
  154.     asm    mov    ah,0x6
  155.     asm    call [xms_entry]
  156.     asm    mov    xms_error,bl
  157.     asm    not ax
  158.     asm    inc ax
  159.     asm    inc ax
  160. }
  161.  
  162.  
  163. int
  164. xms_a20_status(void) {
  165.     asm    mov    ah,0x7
  166.     asm    call [xms_entry]
  167.     asm    mov    xms_error,bl
  168. }
  169.  
  170. int
  171. xms_emb_allocate(emb_size_K_t siz) {
  172.     asm    mov ah,0x9
  173.     asm    mov    dx,siz
  174.     asm    call [xms_entry]
  175.     asm    mov    xms_error,bl
  176.     asm    or    ax,ax            /* allocation succeed? */
  177.     asm    jz alloc_failed        /* no, return 0           */
  178.     asm    xchg ax,dx            /* yes, return handle */
  179.     asm    jmp done
  180. alloc_failed:
  181.     asm    mov    ax,-1
  182. done:;
  183. }
  184.  
  185. int
  186. xms_emb_free(emb_handle_t handle) {
  187.     asm    mov    ah,0x0a
  188.     asm    mov dx,handle
  189.     asm    call [xms_entry]
  190.     asm    mov    xms_error,bl
  191.     asm    not ax
  192.     asm    inc ax
  193.     asm    inc ax
  194. }
  195.  
  196. emb_handle_info *
  197. xms_get_emb_handle_info(emb_handle_t handle) {
  198.     static emb_handle_info info;
  199.     unsigned char locks,handles;
  200.     emb_size_K_t siz;
  201.     asm    mov    ah,0x0e
  202.     asm    mov    dx,handle
  203.     asm    call [xms_entry]
  204.     asm    or ax,ax        /* did call fail? */
  205.     asm    jz call_failed
  206.     asm    mov    locks,bh
  207.     asm    mov handles,bl
  208.     asm    mov    siz,dx
  209.     asm    jmp short done
  210.     call_failed:
  211.     asm    mov    xms_error,bl
  212.     asm    mov ax,1
  213.     asm    mov word ptr siz,-1
  214.     done:
  215.     info.lock_count = locks;
  216.     info.handles_available = handles;
  217.     info.block_size = siz;
  218.     return siz == 0xffff ? NULL : &info;
  219. }
  220.  
  221. int
  222. xms_emb_resize(emb_handle_t handle,emb_size_K_t siz) {
  223.     asm    mov    ah,0xf
  224.     asm    mov    bx,siz
  225.     asm    mov    dx,handle
  226.     asm    call [xms_entry]
  227.     asm    mov    xms_error,bl
  228.     asm    not ax
  229.     asm    inc ax
  230.     asm    inc ax
  231. }
  232.  
  233. int
  234. xms_move_emb(xms_move_param *x) {
  235.     xms_move_param far *x2 = (xms_move_param far *)x;
  236.     asm    push    ds
  237.     asm    mov    ah,0xb
  238.     asm    lds    si,x2
  239.     asm    call [xms_entry]
  240.     asm    mov    xms_error,bl
  241.     asm    not ax
  242.     asm    inc ax
  243.     asm    inc ax
  244. }
  245.  
  246. emb_off_t
  247. xms_lock_emb(emb_handle_t handle) {
  248.     asm    mov    ah,0x0c
  249.     asm    mov    dx,handle
  250.     asm    call [xms_entry]
  251.     asm    cmp    ax,0
  252.     asm    je    lock_failed
  253.     asm    mov    ax,bx        /* return value in dx:ax */
  254.     asm    mov    xms_error,0
  255.     asm    jmp    lock_done
  256. lock_failed:
  257.     asm    mov    dx,ax
  258.     asm    mov    xms_error,bl
  259. lock_done:
  260.     ;
  261. }
  262.  
  263. int
  264. xms_unlock_emb(emb_handle_t handle) {
  265.     asm    mov    ah,0x0d
  266.     asm    mov    dx,handle
  267.     asm    call [xms_entry]
  268.     asm    mov    xms_error,bl
  269.     asm    not ax
  270.     asm    inc ax
  271.     asm    inc ax
  272. }
  273.  
  274. umb_info *
  275. xms_umb_allocate(umb_size_t siz) {
  276.     static umb_info x;
  277.     unsigned segment,block_size,status;
  278.     asm    mov    ah,0x10
  279.     asm    mov    dx,siz
  280.     asm    call [xms_entry]
  281.     asm    mov xms_error,bl
  282.     asm    mov segment,bx
  283.     asm    mov    block_size,dx
  284.     asm    or ax,ax
  285.     asm    jnz done
  286.     asm    mov    segment,ax
  287. done:
  288.     x.segment = segment;
  289.     x.block_size = block_size;
  290.     return segment != 0 ? &x : NULL;
  291. }
  292.  
  293. int
  294. xms_umb_free(umb_segment_t segment) {
  295.     asm    mov    ah,0x11
  296.     asm    mov    dx,segment
  297.     asm    call [xms_entry];
  298.     asm    mov    xms_error,bl
  299.     asm    not ax
  300.     asm    inc ax
  301.     asm    inc ax
  302. }
  303.  
  304.